package com.gmail.at.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.Size;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

/**
 * The persistent class for the passenger database table.
 * 
 */
@Entity
public class Passenger implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private int passengerID;

	private String login;
	
	private int enabled = 1;

	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	@DateTimeFormat(iso = ISO.DATE)
	private DateTime passengerDateOfBirth;

	private String passengerName;

	private String passengerSurname;

	private String password;
	
	private int version;

	@Version
	public int getVersion() {
		return version;
	}

	@Version
	public void setVersion(int version) {
		this.version = version;
	}

	// bi-directional many-to-one association to Ticket
	@OneToMany(mappedBy = "passenger", cascade = CascadeType.ALL, orphanRemoval = true)
	private List<Ticket> tickets;

	public Passenger() {
	}

	public int getPassengerID() {
		return this.passengerID;
	}

	public void setPassengerID(int passengerID) {
		this.passengerID = passengerID;
	}

	@NotEmpty(message="{validation.login.NotEmpty.message}")
	@Size(min=3, max=60, message="{validation.login.Size.message}")
	public String getLogin() {
		return this.login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public DateTime getPassengerDateOfBirth() {
		return this.passengerDateOfBirth;
	}

	public void setPassengerDateOfBirth(DateTime passengerDateOfBirth) {
		this.passengerDateOfBirth = passengerDateOfBirth;
	}

	@NotEmpty(message="{validation.firstname.NotEmpty.message}")
	@Size(min=3, max=60, message="{validation.firstname.Size.message}")
	public String getPassengerName() {
		return this.passengerName;
	}

	public void setPassengerName(String passengerName) {
		this.passengerName = passengerName;
	}

	@NotEmpty(message="{validation.lastname.NotEmpty.message}")
	@Size(min=2, max=40, message="{validation.lastname.Size.message}")
	public String getPassengerSurname() {
		return this.passengerSurname;
	}

	public void setPassengerSurname(String passengerSurname) {
		this.passengerSurname = passengerSurname;
	}

	@NotEmpty(message="{validation.password.NotEmpty.message}")
	@Size(min=2, max=40, message="{validation.password.Size.message}")
	public String getPassword() {
		return this.password; 
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@JsonIgnore
	public List<Ticket> getTickets() {
		return this.tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	@Transient
	public String getBirthDateString() {
		String birthDateString = "";
		if (passengerDateOfBirth != null)
			birthDateString = org.joda.time.format.DateTimeFormat.forPattern(
					"yyyy-MM-dd").print(passengerDateOfBirth);
		return birthDateString;
	}

	public String toString() {
		return "Passenger - Id: " + passengerID + ", First name: " + passengerName
				+ ", Last name: " + passengerSurname + ", Birthday: " + passengerDateOfBirth;
	}

	public int getEnabled() {
		return enabled;
	}

	public void setEnabled(int enabled) {
		this.enabled = enabled;
	}
}